<?php

namespace common\models\mongo;

use Yii;
use youmbe\sports\NanoSports;

/**
 * This is the model class for collection "match_lineup".
 *
 * @property \MongoDB\BSON\ObjectID|string $_id
 * @property mixed $id
 * @property mixed $channel_id
 * @property mixed $teams
 * @property mixed $matchevents
 * @property mixed $info
 * @property mixed $history
 * @property mixed $goal_distribution
 * @property mixed $injury
 * @property mixed $table
 * @property mixed $created_at
 * @property mixed $updated_at
 */
class MatchAnalysis extends \common\db\MongoActiveRecord {

    /**
     * @var boolean 是否强制刷新
     */
    public $forceRefresh = false;

    /**
     * {@inheritdoc}
     */
    public static function collectionName() {
        return 'match_analysis';
    }

    /**
     * {@inheritdoc}
     */
    public function attributes() {
        return [
            '_id',
            'id',
            'channel_id',
            'teams',
            'matchevents',
            'info',
            'history',
            'goal_distribution',
            'injury',
            'table',
            'created_at',
            'updated_at'
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['id', 'channel_id', 'teams', 'matchevents', 'info', 'history', 'goal_distribution', 'injury', 'table', 'created_at', 'updated_at'], 'safe']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            '_id' => 'ID',
            'id' => 'Id',
            'channel_id' => 'Channel ID',
            'teams' => 'teams',
            'matchevents' => 'matchevents',
            'info' => 'info',
            'history' => 'history',
            'goal_distribution' => 'goal_distribution',
            'injury' => 'injury',
            'table' => 'table',
        ];
    }

    public function checkRefresh() {
        if ($this->isNewRecord) {
            return true;
        }
        if ($this->updated_at + 120 < time()) {
            return $this->forceRefresh;
        }
        return false;
    }

    /**
     * 查询数据信息
     * @params array|mixed $condition
     * @return static
     */
    public static function findCurrent($condition, $force = false) {
        $model = static::findOrNew($condition);
        $model->forceRefresh = $force;
        if ($model->checkRefresh()) {
            $data = $model->loadChannelData();
            if (!$data) {
                return $model->isNewRecord ? null : $model;
            }
            $model->attributes = $data;
            $model->save();
        }
        return $model;
    }

    public function loadChannelData() {
        $sports = new NanoSports();
        return $sports->analysis($this->channel_id);
    }

    public function fields() {
        return [
            'teams',
            'matchevents',
            'info',
            'history',
            'goal_distribution',
            'injury',
            'table',
        ];
    }

}
